/*-------------------------------------------------------------------------------*/
/*------- The Heterogeneous Effects of Monetary Policy on Labor Income: ---------*/
/*-------     Disentangling the Extensive and Intensive Margins         ---------*/
/*-------         This code produces Table A4 in the Appendix           ---------*/
/*-------------------------------------------------------------------------------*/

drop _all
clear matrix
global DIR "C:\Replication package"
cd "${DIR}"

use "FINAL_DATABASE.dta", clear
set httpproxy on

*---------------------------*
*----- Data processing -----*
*---------------------------*

xtset id year
sum total_after_tax, det
rename *income* *inc*
rename *labour* *labor*

*** Deflating income
bysort year: egen mean_inc = mean(total_after_tax)
gen mean_inc_def = mean_inc/ipch_2018
//
gen labor_inc_def = labor_inc/ipch_2018

*** Situation Actif (emploi + chomage)
replace actif = 0 if actif==2

xtset id year
bysort id: gen situa_actif = 0
replace situa_actif = 1 if (situa_new==1 | situa_new==2 | situa_new==4) 
replace situa_actif = 1 if (L.situa_new==1 | situa_new==2 | L.situa_new==4) & L.id==id
replace situa_actif = 1 if (F.situa_new==1 | situa_new==2 | F.situa_new==4) & F.id==id
tab situa_actif

gen situa_revsal = 0
replace situa_revsal = 1 if labor_inc_def!=0
replace situa_revsal = 1 if L.labor_inc_def!=0 & L.id==id
replace situa_revsal = 1 if F.labor_inc_def!=0 & F.id==id
replace situa_revsal = 1 if situa_new==4
replace situa_revsal = 1 if L.situa_new==4 & L.id==id
replace situa_revsal = 1 if F.situa_new==4 & F.id==id
tab situa_revsal

*** Benchmark labor income
gen base_income = .
replace base_income = L.labor_inc_def
replace base_income = L2.labor_inc_def if L.labor_inc_def==.

*** Deciles
sum labor_inc_def if situa_actif==1
sum labor_inc_def if situa_actif==1 & L.labor_inc_def!=.
sum labor_inc_def if situa_actif==1 & base_income!=.
xtset id year
xtile decile_actif = base_income if situa_actif==1, nq(10)
tab decile_actif

*** Labor income changes
xtset id year
bysort id: gen AC0labor = labor_inc_def - base_income
replace AC0labor = . if decile_actif==.
bysort id: gen AC1labor = F1.labor_inc_def - base_income
replace AC1labor = . if decile_actif==.

*** Removing outliers
cap drop nooutlier AC0labor_p1 AC0labor_p99
sum AC0labor, det
bysort year: egen AC0labor_p1 = pctile(AC0labor), p(1)
bysort year: egen AC0labor_p99 = pctile(AC0labor), p(99)
gen nooutlier = 1
replace nooutlier = 0 if (AC0labor<AC0labor_p1 | AC0labor>AC0labor_p99)

*** Our sample
cap drop sample
gen sample = .
replace sample = 1 if AC0labor!=. &  AC1labor!=. & nooutlier==1
tab sample

*** Monetary policy shocks
cap drop mp_shock mp_shock1
egen mp_shock1 = std(jk19_mpsign)
gen mp_shock = -1*mp_shock1
sum jk19_mpsign mp_shock1 mp_shock 


*----------------------------------------------*
*----------- Overall labor income ------------*
*----------------------------------------------*

xtset id year

*** Overall effect
eststo clear
eststo: quiet reg AC0labor mp_shock if decile_actif!=. & sample==1, vce(clus id)
eststo: quiet reg AC0labor mp_shock if decile_actif<=4 & sample==1, vce(clus id)
eststo: quiet reg AC0labor mp_shock if decile_actif>=5 & decile_actif<=8 & sample==1, vce(clus id)
eststo: quiet reg AC0labor mp_shock if decile_actif>=9 & decile_actif!=. & sample==1, vce(clus id)
esttab, nocons compress keep(mp_shock) star(** 0.05 *** 0.01)
esttab using TabA4a.tex, b(1) t(2) br noeqlines nogaps nolines nocons compress ///
keep(mp_shock) stats(N) star(** 0.05 *** 0.01) replace


*** In % of average income
sum base_income if decile_actif!=. & sample==1 
global mean_all = r(mean)
disp $mean_all
sum base_income if decile_actif<=4 & sample==1
global mean_b50 = r(mean)
disp $mean_b50
sum base_income if decile_actif>=5 & decile_actif<=8 & sample==1
global mean_m40 = r(mean)
disp $mean_m40
sum base_income if decile_actif>=9 & decile_actif!=. & sample==1
global mean_t10 = r(mean)
disp $mean_t10
cap drop AC0labor_all AC0labor_b50 AC0labor_m40 AC0labor_t10
gen AC0labor_all = AC0labor*100/$mean_all if decile_actif!=. & sample==1
gen AC0labor_b50 = AC0labor*100/$mean_b50 if decile_actif<=4 & sample==1
gen AC0labor_m40 = AC0labor*100/$mean_m40 if decile_actif>=5 & decile_actif<=8 & sample==1
gen AC0labor_t10 = AC0labor*100/$mean_t10 if decile_actif>=9 & decile_actif!=. & sample==1
//
eststo clear
eststo All: quiet reg AC0labor_all mp_shock if decile_actif!=. & sample==1, vce(clus id)
eststo B50: quiet reg AC0labor_b50 mp_shock if decile_actif<=4 & sample==1, vce(clus id)
eststo M40: quiet reg AC0labor_m40 mp_shock if decile_actif>=5 & decile_actif<=8 & sample==1, vce(clus id)
eststo T10: quiet reg AC0labor_t10 mp_shock if decile_actif>=9 & decile_actif!=. & sample==1, vce(clus id)
esttab All B50 M40 T10, nocons compress keep(mp_shock) star(** 0.05 *** 0.01)
esttab using TabA4b.tex, b(3) t(2) br noeqlines nogaps nolines nocons compress ///
keep(mp_shock) stats(N) star(** 0.05 *** 0.01) replace


